package com.example.module_detail.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import com.bawei.common.entity.CommendEntity
import com.bawei.common.entity.ReplyCommendEntity
import com.example.detail.databinding.ItemCommendChildBinding
import com.example.detail.databinding.ItemCommendGroupBinding
import com.example.image.ImgOptions
import com.example.image.LoadImageUtil

class CommendListAdapter constructor(private val list:MutableList<CommendEntity>):BaseExpandableListAdapter(){
    override fun getGroupCount(): Int = list.size

    override fun getGroup(p0: Int): CommendEntity = list[p0]
    override fun getGroupId(p0: Int): Long = list[p0].id.toLong()
    override fun getGroupView(p0: Int, p1: Boolean, p2: View?, p3: ViewGroup?): View {
        var groupEntity = list[p0]

        val groupDataBinding = ItemCommendGroupBinding.inflate(LayoutInflater.from(p3?.context),p3,false)

        LoadImageUtil.loadUrl(groupDataBinding.root.context,groupEntity.userlogo,groupDataBinding.civUserIcon,
            ImgOptions.Builder.isCircle(true).build())
        groupDataBinding.tvNickName.text = if (groupEntity.nickname.isNullOrEmpty()) "小爱吃鱼" else groupEntity.nickname
        groupDataBinding.tvContent.text = groupEntity.content
        groupDataBinding.tvCreateTime.text = "发布时间:${groupEntity.createtime}"
        groupDataBinding.tvExpand.text = if (p1) "收起回复" else "展开回复"
        return groupDataBinding.root
    }



    override fun getChildrenCount(p0: Int): Int = list[p0].replytotal



    override fun getChild(p0: Int, p1: Int): ReplyCommendEntity = list[p0].replyList[p1]



    override fun getChildId(p0: Int, p1: Int): Long = list[p0].replyList[p1].id.toLong()

    override fun hasStableIds(): Boolean = true



    override fun getChildView(p0: Int, p1: Int, p2: Boolean, p3: View?, p4: ViewGroup?): View {
       val childEntity = list[p0].replyList[p1]
       val childDataBinding = ItemCommendChildBinding.inflate(LayoutInflater.from(p4?.context),p4,false)
       LoadImageUtil.loadUrl(childDataBinding.root.context,childEntity.userlogo,childDataBinding.civUserIcon,ImgOptions.Builder.isCircle(true).build())
       childDataBinding.tvNickName.text = if(childEntity.nickname.isNullOrEmpty()) "买核弹的小女孩" else childEntity.nickname
       childDataBinding.tvContent.text = childEntity.content
       childDataBinding.tvReplyTime.text = "回复时间${childEntity.createtime}"
       return childDataBinding.root
    }

    override fun isChildSelectable(p0: Int, p1: Int): Boolean = true

}